<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Debug extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        echo $_SERVER['REQUEST_METHOD'];
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            // 获取表单数据
         
            $data = $this->request->post();
        
            print_r($data);
            
            
        
            // TCP服务器的地址和端口
            $tcpHost = '127.0.0.1';
            $tcpPort = 2090;
        
            // 创建TCP socket
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            if (!$socket) {
                die('Unable to create socket');
            }
        
            // 连接到TCP服务器
            $result = socket_connect($socket, $tcpHost, $tcpPort);
            if (!$result) {
                die('Unable to connect to TCP server');
            }
            
          
        
            // 发送数据到TCP服务器
            $bytesSent = socket_write($socket, json_encode($data));
            if ($bytesSent === false) {
                die('Unable to send data');
            }
        
            // 接收TCP服务器的响应（如果需要）
            $response = socket_read($socket, 2048);
            echo "Response from TCP server: $response";
        
            // 关闭socket
            socket_close($socket);
        } else {
            echo "Invalid request method";
        }


        return $this->view->fetch();
    }

}
